"use strict";
exports.__esModule = true;
function getCurrentPages() {
    return [{ route: 'currentPage' }];
}
var Store = /** @class */ (function () {
    function Store(parameters) {
        this._store = new Map();
        this.listenerList = new Map();
        this.store = parameters;
    }
    Store.prototype.get = function (key) {
        return this._store.get(key);
    };
    Store.prototype.has = function (key) {
        return this._store.has(key);
    };
    Store.prototype["delete"] = function (key) {
        return this._store["delete"](key);
    };
    Store.prototype.set = function (obj, val) {
        if (typeof obj === "string" && val !== undefined) {
            this.triggerListener("set", obj, this._store.get(obj));
            this._store.set(obj, val);
        }
        else if (typeof obj === "object") {
            for (var key in obj) {
                this.triggerListener(key, obj[key], this._store.get(key));
                this._store.set(key, obj[key]);
            }
        }
    };
    Store.prototype.on = function (key, listener) {
        var listenerConfig = {
            handler: Function,
            executed: false,
            pageUrl: this.getPageUrl()
        };
        if (typeof listener === "function") {
            listenerConfig.handler = listener;
        }
        else {
            listenerConfig = listener;
        }
        this.setListener(key, listenerConfig);
    };
    Store.prototype.setListener = function (key, listener) {
        if (this.hasListener(key)) {
            this.listenerList.get(key).push(listener);
        }
        else {
            this.listenerList.set(key, [listener]);
        }
    };
    Store.prototype.hasListener = function (key) {
        return this.listenerList.has(key);
    };
    Store.prototype.getCurrPage = function () {
        var pageList = getCurrentPages();
        return pageList[pageList.length - 1] || { route: '' };
    };
    Store.prototype.getPageUrl = function () {
        return this.getCurrPage().route;
    };
    Store.prototype.triggerListener = function (key, newVal, oldVal) {
        var currPageUrl = this.getPageUrl();
        if (this.hasListener(key)) {
            this.listenerList.get(key).filter(function (x) {
                var isFilter = false;
                if (!x.inPage) {
                    isFilter = true;
                }
                if (x.inPage && currPageUrl === x.pageUrl) {
                    isFilter = true;
                }
                if (x.once && x.executed) {
                    isFilter = false;
                }
                return isFilter;
            }).forEach(function (x) {
                x.executed = true;
                x.handler(newVal, oldVal);
            });
        }
    };
    Store.prototype.createProxy = function (store) {
        for (var item in store) {
            if (typeof store[item] === 'object') {
                store[item] = this.proxyStore(store[item]);
            }
        }
        return this.proxyStore(store);
    };
    Store.prototype.proxyStore = function (store) {
        return new Proxy(store, {
            get: function (target, prop) {
                return Reflect.get(target, prop);
            },
            set: function (target, prop, value) {
                console.warn('Warning:请使用set方法操作store');
                return true;
            }
        });
    };
    return Store;
}());
exports["default"] = Store;
